<template>
  <div class="modal-overlay" @click.self="handleOverlayClick">
    <div class="modal-container">
      <div class="modal-header">
        <h3>{{ title }}</h3>
        <button class="close-btn" @click="close">×</button>
      </div>
      <div class="modal-body">
        <slot>{{ content }}</slot>
      </div>
      <div class="modal-footer">
        <button class="btn btn-cancel" @click="cancel">取消</button>
        <button class="btn btn-confirm" @click="confirm">确定</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { useModalContext } from '../hooks/useModal';

// 使用新的hook获取上下文
const { modalProps, confirm, cancel, close } = useModalContext();

// 计算属性处理props
const title = computed(() => modalProps?.title || '默认标题');
const content = computed(() => modalProps?.content || '');

// 点击遮罩层关闭
const handleOverlayClick = () => {
  if (modalProps?.closeOnOverlay !== false) {
    close();
  }
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-container {
  background: white;
  border-radius: 8px;
  min-width: 400px;
  max-width: 80vw;
  max-height: 80vh;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #333;
}

.modal-body {
  padding: 20px;
  max-height: 60vh;
  overflow-y: auto;
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.btn {
  padding: 8px 16px;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  background: white;
  cursor: pointer;
  font-size: 14px;
}

.btn-confirm {
  background: #1890ff;
  border-color: #1890ff;
  color: white;
}

.btn-confirm:hover {
  background: #40a9ff;
}

.btn-cancel:hover {
  border-color: #40a9ff;
  color: #40a9ff;
}
</style>